function normalizeTitle(title, height)
{
    var t = 4.4 + (40.6 - height) / 2;
    var out = "<div id='title_div' unselectable='on' class='unselectable' style='width: 590px; overflow:hidden; position:relative;top:" + t + "px; margin:auto; visibility:hidden; height:17px;'><font id='title' unselectable='on' class='unselectable movieTitleClass'>";
    out += title;
    out += "</font></div>";
    return out;
}
function dynamicTitle(title, length, height)
{
    var t = 4.4 + (40.6 - height) / 2;
    var space = 200;
    var out = "<div id='title_div' unselectable='on' class='unselectable' style='top:" + t + "px; text-align:left; width: 590px; overflow:hidden; position:relative; margin:auto; height:17px;'><font id='title1' unselectable='on' class='unselectable movieTitleClass' style='position:absolute; width:" + length + "px; left:" + space + "px'>" + title + "</font><font id='title2' unselectable='on' class='unselectable movieTitleClass' style='position:absolute;width:" + length + "px; left:" + (space * 2 + length) +"px'>" + title + "</font></div>";
    return out;
}

function getTitleLength(title)
{
    var segs = [];
    var length = 0;
    var i = 0;
    //the title[i] is the i'th character (no mater english or chinese) of title
    while (i < title.length)
    {
        var t = title[i];
        if (ifEnglishLower(t))
        {
            length += 8;
        }
        else if (ifEnglishUpper(t))
        {
            length += 10;
        }
        else
        {
            length += 16;
        }
        segs.push(title.substr(i, 1));
        i = i + 1;
    }
    return length;
}

function getFavListVisible(list)
{
    var length = 0;
    var newlist = "";
    var i = 0;
    while (i < list.length)
    {
        var t = list[i];
        if (ifEnglishLower(t))
        {
            length += 8;
        }
        else if (ifEnglishUpper(t))
        {
            length += 10;
        }
        else
        {
            length += 16;
        }
        i = i + 1;
        newlist = newlist + t;
        if (length >= 80)
        {
            if (i < list.length)
            {
                newlist = newlist + "...";
            }
            break;
        }
    }
    return newlist;
}

function getStringBytesSize(s)
{
    var i = 0;
    var length = 0;
    while (i < s.length)
    {
        var t = s[i];
        if (ifEnglishLower(t) || ifEnglishUpper(t))
        {
            length += 2;
        }
        else
        {
            length += 4;
        }
        i = i + 1;
    }
    return length;
}

function ifEnglishLower(c)
{
    if (c >= 'a' && c <= 'z' || c >= '0' && c <= '9' || c == ' ' || c == '`' || c == '~' || c == '!' || c == '@' || c == '#' || c == '$' || c == '%' || c == '^' || c == '&' || c == '*' || c == '(' || c == ')' || c == '-' || c ==  '_' || c == '+' || c == '=' || c == '{' || c == '[' || c == '}' || c == ']' || c == '\\' || c == '|' || c == ';' || c == ':' || c == '"' || c == '\'' || c == '<' || c == ',' || c == '>' || c == '.' || c == '?' || c == '/')
    {
        return true;
    }
    return false;
}

function ifEnglishUpper(c)
{
    if (c >= 'A' && c <= 'Z')
    {
        return true;
    }
    return false;
}


function clearregist(o)
{
	ifRegist = false;
	o.src = 'ico/login_reg/login_close_click.png';
	$("page_background_regist_filter").setStyle("visibility", "hidden");
	$("page_background_regist").setStyle("visibility", "hidden");
	if ($("registPanel"))
	{
		$("registPanel").destroy();
	}
	if ($("emailActivate"))
	{
		$("emailActivate").destroy();	
	}
    if ($('tutorial_background'))
    {
        $('tutorial_background').destroy();
    }
}

function closeregistMouseOver(o)
{
	o.src = 'ico/login_reg/login_close_mouseon.png';	
}

function closeregistMouseOut(o)
{
	o.src = 'ico/login_reg/login_close_normal.png';	
}
function closeregistMouseDown(o)
{
	o.src = 'ico/login_reg/login_close_click.png';	
}
function closeregistMouseUp(o)
{
	o.src = 'ico/login_reg/login_close_mouseon.png';	
}

function loginButtonMouseOver(o)
{
    if (browser.ie)
    {
        o.style.backgroundImage = "url('ico/login_reg/button_mouseon.png')";
    }
    else
    {
	    o.setStyle('background-image', 'url(ico/login_reg/button_mouseon.png)');	
    }
}

function loginButtonMouseOut(o)
{
    if (browser.ie)
    {
        o.style.backgroundImage = "url('ico/login_reg/button_normal.png')";
    }
    else
    {
	    o.setStyle('background-image', 'url(ico/login_reg/button_normal.png)');	
    }
}

function loginButtonMouseDown(o)
{
    if (browser.ie)
    {
        o.style.backgroundImage = "url('ico/login_reg/button_click.png')";
    }
    else
    {
	    o.setStyle('background-image', 'url(ico/login_reg/button_click.png)');	
    }
}

function loginButtonMouseUp(o)
{
    if (browser.ie)
    {
        o.style.backgroundImage = "url('ico/login_reg/button_mouseon.png')";
    }
    else
    {
	    o.setStyle('background-image', 'url(ico/login_reg/button_mouseon.png)');	
    }
}

function myAlert(message)
{
	if ($('registPanel'))
	{
		$('registPanel').destroy();	
	}
	if ($('loginPanel'))
	{
        $('loginPanel').destroy();
	}
    if ($('emailActivate'))
    {
        $('emailActivate').destroy();
    }
    if ($('changePassword'))
    {
        $('changePassword').destroy();
    }
	$("page_background_regist_filter").setStyle("visibility", "visible");
	$('page_background_regist_filter').set('opacity', 0.8);
	$("page_background_regist").setStyle("visibility", "visible");
    this.emailActivate = new Element('div', {id:"emailActivate"}).addClass('emailActivateClass').inject($("page_background_regist"));
    this.emailActivate.first = new Element('div', {id:"registPanel_first"}).addClass('registPanel_firstClass').inject(this.emailActivate);
    this.emailActivate.first.closebutton_background = new Element('div', {id:"registPanel_first_closebutton_background"}).addClass('registPanel_first_closebutton_backgroundClass').inject(this.emailActivate.first);
    this.emailActivate.first.closebutton_background.innerHTML = "<img src='ico/login_reg/login_close_normal.png' id='regist_close' onclick='clearregist(this)' onmouseover='closeregistMouseOver(this)' onmouseout='closeregistMouseOut(this)' onmousedown='closeregistMouseDown(this)' onmouseup='closeregistMouseUp(this)' />";
    this.emailActivate.logo = new Element('div').addClass('changePass_logoClass').inject($(this.emailActivate));
    this.emailActivate.logo.innerHTML = "<img src='ico/logo4.png' />";
    this.emailActivate.second = new Element('div', {id:'emailActivate_second'}).addClass('warningClass').inject($(this.emailActivate));
    this.emailActivate.second.innerHTML = "<span style='color:#999'>" + message + "</span>";
    this.emailActivate.third = new Element('div', {id:"confirmThird"}).addClass('confirmThirdClass').inject(this.emailActivate);
}


function pageHeight(){ 
    if($j.browser.msie){ 
        return document.compatMode == "CSS1Compat"? document.documentElement.clientHeight : 
            document.body.clientHeight; 
    }else{ 
        return self.innerHeight; 
    } 
}; 
function pageWidth(){ 
    if($j.browser.msie){ 
        return document.compatMode == "CSS1Compat"? document.documentElement.clientWidth : 
            document.body.clientWidth; 
    }else{ 
        return self.innerWidth; 
    } 
}; 
function registButtonMouseOver(o)
{
    if (browser.ie)
    {
        o.style.backgroundImage = "url('ico/login_reg/button_mouseon.png')";
    }
    else
    {
	    o.setStyle('background-image', 'url(ico/login_reg/button_mouseon.png)');	
    }
}
function registButtonMouseUp(o)
{
    if (browser.ie)
    {
        o.style.backgroundImage = "url('ico/login_reg/button_mouseon.png')";
    }
    else
    {
	    o.setStyle('background-image', 'url(ico/login_reg/button_mouseon.png)');	
    }
}

function registButtonMouseOut(o)
{
    if (browser.ie)
    {
        o.style.backgound = "background-image: url('ico/login_reg/button_normal.png')";
    }
    else
    {
	    o.setStyle('background-image', 'url(ico/login_reg/button_normal.png)');	
    }
}

function registButtonMouseDown(o)
{
    if (browser.ie)
    {
        o.style.backgroundImage = "url('ico/login_reg/button_click.png')";
    }
    else
    {
	    o.setStyle('background-image', 'url(ico/login_reg/button_click.png)');	
    }
}

function normalizeEmail(email)
{
    return email.replace(/(^s*)|(s*$)/g, "");
}
